package com.hgk.common.common.util;

import android.content.ContentValues;

import java.lang.reflect.Field;

/**
 * bean转contentValus工具类
 * @author huzhaoqian
 * create 2014.12.2
 */
public class BeanToCVUtil {
	/**
	 * 将一个bean对象转化为contentValues，key为bean的属性名
	 * @param bean
	 * @return
	 */
	public static ContentValues convertBean2CV(Object bean){
		ContentValues cv = new ContentValues();
		Field[] fields = bean.getClass().getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);
			String name = field.getName();
			if("serialVersionUID".equals(name) || "_id".equals(name)){//排除序列化对象自动添加的serialVersionUID属性
				continue;
			}
			if("photoList".equals(name)){//过滤微记录photoList字段
				continue;
			}
			if("tagList".equals(name)){//过滤微记录tagList字段
				continue;
			}
			if("bean".equals(name)){
				continue;
			}
			if("operate_type".equals(name)){//过滤微记录operate_type字段转换
				continue;
			}
			try {
				Object objValue = field.get(bean);
				if(null != objValue){
					if(long.class.equals(field.getType())){
						Long value = (Long) objValue;
						cv.put(name, value);
					}else{
						String value = String.valueOf(objValue);
						cv.put(name, value);
					}					
				}
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return cv;
	}

}